package com.huawei.esdk.uc.sametime.preferences;

import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import com.huawei.esdk.uc.jna.Constants;
import com.huawei.esdk.uc.jna.UCService;
import com.huawei.esdk.uc.sametime.EsdkPluginActivator;
import com.huawei.esdk.uc.sametime.international.Messages;
import com.huawei.esdk.uc.sametime.util.GlobalConfiguration;

/**
 * This class represents a preference page that is contributed to the
 * Preferences dialog. By subclassing <samp>FieldEditorPreferencePage</samp>, we
 * can use the field support built into JFace that allows us to create a page
 * that is small and knows how to save, restore and apply itself.
 * <p>
 * This page is used to modify preferences only. They are stored in the
 * preference store that belongs to the main plug-in class. That way,
 * preferences can be accessed directly via the preference store.
 */

public class CallsSubPreferencePage extends FieldEditorPreferencePage implements
        IWorkbenchPreferencePage
{

    // private RadioGroupFieldEditor answerMode;

    private StringFieldEditor uc_number;

    // private StringFieldEditor number_1;

    // private StringFieldEditor number_2;
    // private StringFieldEditor number_3;

    private Label callTranLinkLabel;

    private Button callTranButton;

    public CallsSubPreferencePage()
    {
        super(GRID);
        setPreferenceStore(EsdkPluginActivator.getDefault()
                .getPreferenceStore());
        setDescription(Messages.CallsSubPreferencePage_DESCRIPTION);
        noDefaultAndApplyButton();
    }

    /**
     * Creates the field editors. Field editors are abstractions of the common
     * GUI blocks needed to manipulate various types of preferences. Each field
     * editor knows how to save and restore itself.
     */
    public void createFieldEditors()
    {

        /*
         * 呼叫应答同振and顺振配置
         */
        // String[][] answerModeValueArray = new String[][]{{
        // Messages.CallsSubPreferencePage_RING_SYNC, "0" },{
        // Messages.CallsSubPreferencePage_RING_SERIAL, "1" }};
        // answerMode = new
        // RadioGroupFieldEditor(PreferenceConstants.P_ANSWERMODE,Messages.CallsSubPreferencePage_SELECT_ANSWER_MODE,
        // 2, answerModeValueArray, getFieldEditorParent());
        uc_number = new StringFieldEditor(PreferenceConstants.P_USERNAME,
                Messages.CallsSubPreferencePage_BIND_UC_ACCOUNT,
                getFieldEditorParent());
        // number_1 = new StringFieldEditor(PreferenceConstants.P_NUMBER_1,
        // Messages.CallsSubPreferencePage_PHONE_NUM_1,
        // getFieldEditorParent());
        // number_2 = new StringFieldEditor(PreferenceConstants.P_NUMBER_2,
        // Messages.CallsSubPreferencePage_PHONE_NUM_2,
        // getFieldEditorParent());
        // number_3 = new StringFieldEditor(PreferenceConstants.P_NUMBER_3,
        // Messages.CallsSubPreferencePage_PHONE_NUM_3,
        // getFieldEditorParent());

        // number_1.setEmptyStringAllowed(true);
        // number_2.setEmptyStringAllowed(true);
        // number_3.setEmptyStringAllowed(true);

        // addField(answerMode);
        addField(uc_number);
        // addField(number_1);
        // addField(number_2);
        // addField(number_3);

        // 呼叫前转规则页面打开
        callTranLinkLabel = new Label(getFieldEditorParent(), SWT.NONE);
        callTranLinkLabel
                .setText(Messages.CallsSubPreferencePage_CALL_TRANSFER);
        callTranButton = new Button(getFieldEditorParent(), SWT.PUSH | SWT.NONE);
        callTranButton
                .setText(Messages.CallsSubPreferencePage_SET_CALL_FORWARD_RULE);
        callTranButton.addSelectionListener(new SelectionAdapter()
        {
            public void widgetSelected(SelectionEvent e)
            {
                boolean isOpened = false;
                int openResult = UCService.getInstance().UC_SDK_OpenPortal(
                        Constants.PortalType.SelfMgr_Portal);
                isOpened = (Constants.UCServiceRetvCode.UC_SDK_Success == openResult);
                if (!isOpened)
                {
                    setErrorMessage(Messages.CallsSubPreferencePage_OPEN_CALL_FORWARD_PAGE_FAIL);
                }
            }
        });

        boolean isUCLogin = GlobalConfiguration.getInstance().isUCLogin();
        if (!isUCLogin)
        {
            // answerMode.setEnabled(false, getFieldEditorParent());
            // number_1.setEnabled(false, getFieldEditorParent());
            // number_2.setEnabled(false, getFieldEditorParent());
            // number_3.setEnabled(false, getFieldEditorParent());
            callTranLinkLabel.setEnabled(false);
            callTranButton.setEnabled(false);
        }
        uc_number.setEnabled(false, getFieldEditorParent());

        adjustGridLayout();
    }

    /**
     * 提交“确定”或“应用”按钮，覆盖FieldEditorPreferencePage.performOk方法
     */
    public boolean performOk()
    {
        super.performOk();
        // String answerModeValue =
        // answerMode.getPreferenceStore().getString(PreferenceConstants.P_ANSWERMODE);
        // String uc_number_Value = uc_number.getStringValue();
        // String number_1_Value = number_1.getStringValue();
        // String number_2_Value = number_2.getStringValue();
        // String number_3_Value = number_3.getStringValue();
        // String[] numbers = new String[4];
        // numbers[0] = uc_number_Value;
        // numbers[1] = number_1_Value;
        // numbers[2] = number_2_Value;
        // numbers[3] = number_3_Value;
        //
        // /*
        // * 这里调用UC SDK的设置同振、顺振的服务接口
        // */
        // boolean resAnsmod =
        // UCSettingsService.setAnswerMode(answerModeValue,numbers);
        // if (!resAnsmod)
        // {
        // setErrorMessage(Messages.CallsSubPreferencePage_SET_RING_SERIAL_FAIL);
        // }
        // return resAnsmod;
        return true;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
     */
    public void init(IWorkbench workbench)
    {
    }

}